const request = require('../../utils/api')
const fetch = require('node-fetch');
const BASE_URL = 'http://www.chehangjia.cn:8080/chj';
// const BASE_URL = 'http://localhost:8080/chj';
const FormData = require('form-data');

class Generic {
    fetch(config = {}) {
        config.url = BASE_URL + config.url;
        return request({ ...config })
    }

    get(url, params = {}) {
        return this.fetch({ url, params })
    }

    postBody(url, body = {}, header = {}) {
        return this.fetch({ url, body, header })
    }

    postFormData (url, params = {}, header = {}) {
        // console.log(params);
        let format = new FormData();
        for (const temp in params) {
            format.append(temp, params[temp]);
        }
        // console.log(format);
        return fetch(BASE_URL + url, {
            headers: format.getHeaders(),
            method: 'POST',
            body: format
        })
    }

}

module.exports = Generic;
